package authoritah.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class EventType {
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	//////////////////////
	// Database methods //
	//////////////////////
	
	/** 
	 * Retrieves an event type from the database file. If no event with supplied id exists, returns null.
	 * @param id
	 * @return
	 */
	public static EventType getEventType(int id){
		EventType eventType = new EventType();
		try {
            ParityDB db = ParityDB.getInstance();
            
            Connection conn = db.getConn();
            
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM event_type WHERE id=?");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            
            // Iterate the result set, printing each column
            while (rs.next()) {            	
                eventType.setId(rs.getInt("id"));
                eventType.setName(rs.getString("name"));
            }            
            // Close the connection
            conn.close();
            
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return eventType;
	}
	
	/**
	 * Stores an event type into the database.
	 * @param eventType
	 */
	public static void createEventType(EventType eventType){
		try {
			ParityDB db = ParityDB.getInstance();
            
            Connection conn = db.getConn();
            
            PreparedStatement ps = conn.prepareStatement("INSERT INTO event_type (name) VALUES (?)");
            ps.setString(1, eventType.getName());
            ps.executeUpdate();
            
            // Close the connection
            conn.close();
        }
        catch (Exception e) {
        	e.printStackTrace();
        }
	}
	
	/**
	 * Stores an event type into the database.
	 * @param eventType
	 */
	public static void updateEventType(EventType eventType){
		try {
			ParityDB db = ParityDB.getInstance();
            
            Connection conn = db.getConn();
            
            PreparedStatement ps = conn.prepareStatement("UPDATE event_type SET name=? WHERE id=?");
            ps.setString(1, eventType.getName());
            ps.setInt(2, eventType.getId());
            ps.executeUpdate();
            
            // Close the connection
            conn.close();
        }
        catch (Exception e) {
        	e.printStackTrace();
        }
	}
	
	/**
	 * Stores an event type into the database.
	 * @param eventType
	 */
	public static void removeEventType(EventType eventType){
		try {
			ParityDB db = ParityDB.getInstance();
            
            Connection conn = db.getConn();
            
            PreparedStatement ps = conn.prepareStatement("DELETE FROM event_type WHERE id=?");
            ps.setInt(1, eventType.getId());
            ps.executeUpdate();
            
            // Close the connection
            conn.close();
        }
        catch (Exception e) {
        	e.printStackTrace();
        }
	}
	
	/**
	 * Retrieves all events in the database.
	 * @return All events currently in the database.
	 */
	public static ArrayList<EventType> getAllEventTypes() {
		ArrayList<EventType> events = new ArrayList<EventType>();
		try {
			ParityDB db = ParityDB.getInstance();            
            Connection conn = db.getConn();
            
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM event_type");
            ResultSet rs = ps.executeQuery();
            
            // Iterate the result set, adding to the collection.
            while (rs.next()) {
            	EventType eventType = new EventType();
                eventType.setId(rs.getInt("id"));
                eventType.setName(rs.getString("name"));
                events.add(eventType);
            }
            
            // Close the connection
            conn.close();
            
        }
        catch (Exception e) {
        	e.printStackTrace();
        }
        return events;
	}
	
	public String toString() {
		return name;
	}
}
